<?php

/**
 * @category model
 * @name $Customer
 * @todo manage Customer model
 * @author HungPV <phamvanhung0818@gmail.com>
 */
class Customer extends DataMapper {

    public function __construct($id = NULL) {
        parent::__construct($id);
    }

    public function valid_register($email, $phone) {
        $customer = new Customer();
        $customer->where('email', $email)->get();
        $json = array();
        if ($customer->result_count() > 0) {
            if ($customer->status == Customer_status::Blocked) {
                $json = array(
                    'status' => Customer_status::Blocked
                );
            } else if ($customer->status == Customer_status::NotActive) {
                $json = array(
                    'status' => Customer_status::NotActive
                );
            } else {
                $json = array(
                    'status' => Customer_status::Actived
                );
            }
        } else {
            $customer = new Customer();
            $customer->where('phone', $phone)->get();
            if ($customer->result_count() > 0) {
                $json = array(
                    'phone' => 'exists'
                );
            } else {
                $json = array(
                    'register' => 'new'
                );
            }
        }

        echo json_encode($json);
    }

    function send_otp_register($phone) {
        //Generate otp
        $code = rand(100000, 999999);
        $message = 'Ma kich hoat: ' . $code . '. Nhap ma kich hoat va bam Kich hoat de hoan tat dang ky tai khoan. Cam on Quy Khach.';

        //Send message
        //Save to database
        $otp = new Otp();
        $otp->phone = $phone;
        $otp->message = $message;
        $otp->code = $code;
        $otp->date = date('Y-m-d H:i:s');
        $otp->type = Otp_type::Register;
        $otp->save();
    }

    function get_otp_register($phone) {
        $otp = new Otp();
        $otp->where('phone', $phone)->get();

        $ret_arr = array(
            'err' => 1,
            'message' => 'no_otp'
        );       
        
        
        if ($otp->result_count() > 0) {
            
            $minutes_to_add = 5;
            $date = new DateTime($otp->date);
            $time = $date->add(new DateInterval('PT' . $minutes_to_add . 'M'));
            
            if (strtotime(date('Y-m-d H:i:s')) > strtotime($time->format('Y-m-d H:i:s'))) {
                $ret_arr = array(
                    'err' => 1,
                    'message' => 'expired'
                );
            } else {
                $ret_arr = array(
                    'err' => 0,
                    'message' => 'success',
                    'code' => $otp->code
                );
            }
        } else {
            $ret_arr = array(
                'err' => 1,
                'message' => 'no_otp'
            );
        }
        return $ret_arr;
    }

}

?>